<?php
/**
 * @author qingyu8@staff.weibo.com
 * @date 2018/06/08
 */
namespace common\logic;

use yii;

require_once Yii::$app->vendorPath . '/alipay-sdk-PHP-3.0.1/AopSdk.php';
require_once Yii::$app->vendorPath . '/alipay-sdk-PHP-3.0.1/service/AlipayTradeService.php';
require_once Yii::$app->vendorPath . '/alipay-sdk-PHP-3.0.1/buildermodel/AlipayTradePagePayContentBuilder.php';
require_once Yii::$app->vendorPath . '/alipay-sdk-PHP-3.0.1/buildermodel/AlipayTradeQueryContentBuilder.php';
require_once Yii::$app->vendorPath . '/alipay-sdk-PHP-3.0.1/buildermodel/AlipayTradeRefundContentBuilder.php';
require_once Yii::$app->vendorPath . '/alipay-sdk-PHP-3.0.1/buildermodel/AlipayTradeFastpayRefundQueryContentBuilder.php';

class Alipay {
    /**
     * 订单尚未支付
     * @param string $out_trade_no 商户订单号，商户网站订单系统中唯一订单号
     * @param string $trade_no 支付宝交易号
     */
    public static function query($out_trade_no = '', $trade_no = '') {
        //
        $out_trade_no = trim($_POST['WIDTQout_trade_no']);
        //
        $trade_no = trim($_POST['WIDTQtrade_no']);
        //请二选一设置
        //构造参数
        $RequestBuilder = new \AlipayTradeQueryContentBuilder();
        $out_trade_no && $RequestBuilder->setOutTradeNo($out_trade_no);
        $trade_no && $RequestBuilder->setTradeNo($trade_no);

        $config = Yii::$app->params['alipay'];
        $aop = new \AlipayTradeService($config);

        /**
         * alipay.trade.query (统一收单线下交易查询)
         * @param $builder 业务参数，使用buildmodel中的对象生成。
         * @return $response 支付宝返回的信息
         */
        $response = $aop->Query($RequestBuilder);
        var_dump($response);
    }

    public static function refund(&$order) {
        //支付宝交易号
        //$trade_no = trim($_POST['WIDTRtrade_no']);
        //请二选一设置
        //退款的原因说明
        $refund_reason = '用户申请退款';

        //标识一次退款请求，同一笔交易多次退款需要保证唯一，如需部分退款，则此参数必传
        //$out_request_no = trim($_POST['WIDTRout_request_no']);

        //构造参数
        $RequestBuilder=new \AlipayTradeRefundContentBuilder();
        $RequestBuilder->setOutTradeNo($order['order_id']);//商户订单号，商户网站订单系统中唯一订单号
        //$RequestBuilder->setTradeNo($trade_no);
        //$RequestBuilder->setRefundAmount(0.1);//DEBUG
        $total_amount = Yii::$app->params['payDebug'] && in_array(Yii::$app->user->id, Yii::$app->params['debug_uids']) ? 1 : $order['price'];
        $RequestBuilder->setRefundAmount($total_amount / 100);//需要退款的金额，该金额不能大于订单金额，必填
        //$RequestBuilder->setOutRequestNo($out_request_no);
        $RequestBuilder->setRefundReason($refund_reason);

        $config = Yii::$app->params['alipay'];
        $aop = new \AlipayTradeService($config);

        /**
         * alipay.trade.refund (统一收单交易退款接口)
         * @param $builder 业务参数，使用buildmodel中的对象生成。
         * @return $response 支付宝返回的信息
         */
        $response = $aop->Refund($RequestBuilder);
        Yii::info($response, __METHOD__);
        if ($response->code == 10000) {
            return [
                'err' => 0,
                'data' => [
                    'code' => $response->code,
                    'gmt_refund_pay' => $response->gmt_refund_pay,
                    'trade_no' => $response->trade_no,
                ]
            ];
        } else {
            return [
                'err' => 1,
                'msg' => $response->msg . ':' . $response->sub_msg
            ];
        }
    }
    public static function refundQuery(&$order){
        $config = Yii::$app->params['alipay'];
        //支付宝交易号
        //$trade_no = trim($_POST['WIDRQtrade_no']);

        //构造参数
        $RequestBuilder=new \AlipayTradeFastpayRefundQueryContentBuilder();
        $RequestBuilder->setOutTradeNo($order['order_id']);
        //$RequestBuilder->setTradeNo($trade_no);
        //请求退款接口时，传入的退款请求号，如果在退款请求时未传入，则该值为创建交易时的外部交易号，必填
        $RequestBuilder->setOutRequestNo($order['ext']['alipay']['trade_no']);

        $aop = new \AlipayTradeService($config);

        /**
         * 退款查询   alipay.trade.fastpay.refund.query (统一收单交易退款查询)
         * @param $builder 业务参数，使用buildmodel中的对象生成。
         * @return $response 支付宝返回的信息
         */
        $response = $aop->refundQuery($RequestBuilder);
        var_dump($response);
    }
}
